<?php
namespace app\data_magic\logic;

use think\Db;
use think\db\Connection;
use think\db\Query;


class Database
{
    protected $error;
    public function getError()
    {
        return $this->error;
    }

    public $connect;//数据库连接
    public $database;//数据库名称
    /**
     * 获取information_schema数据库的连接
     * @return Connection
     */
    public function getConnect()
    {
        if($this->connect===null){
            $config=config("database");
            $this->database=$config["database"];
            $config['resultset_type']='array';
            $config["database"]="information_schema";
            $this->connect=Db::connect($config);
        }
        return $this->connect;
    }
    /**
     * @return array
     */
    public function getTableList()
    {
        return $this->getConnect()->table("TABLES")->where(["TABLE_SCHEMA"=>$this->database])->select();
    }
    /**
     * @param string $table_name
     * @return array
     */
    public function getDetail($table_name)
    {
        $connect=$this->getConnect();
        $table=$connect->table("TABLES")->where(["TABLE_SCHEMA"=>$this->database,"TABLE_NAME"=>$table_name])->find();
        $columns=$connect->table("COLUMNS")->where(["TABLE_SCHEMA"=>$this->database,"TABLE_NAME"=>$table_name])->select();
        return [$table,$columns];
    }
    /**
     * @param string $table_name
     * @return array
     */
    public function transToStructure($table_name)
    {
        $schema=$this->getConnect();
        $item=$schema->table("TABLES")->where(["TABLE_SCHEMA"=>$this->database,"TABLE_NAME"=>$table_name])->find();
        $columns=$schema->table("COLUMNS")->where(["TABLE_SCHEMA"=>$this->database,"TABLE_NAME"=>$table_name])->select();
        $table=[
            "name"=>$table_name,
            "title"=> $item["TABLE_COMMENT"] ? $item["TABLE_COMMENT"] : $table_name ,
            "describe"=>$item["TABLE_COMMENT"],
            "primary_key_field"=>""
        ];
        $fields=[];
        $flag=[];
        foreach($columns as $column){
            $column_name=$column["COLUMN_NAME"];
            if($column["COLUMN_KEY"]==="PRI"){
                $flag["primary_key_field"]=$column_name;
            }
            if(in_array($column_name,["user_id","uid","userid"])){
                $flag["user_id_field"]=$column_name;
            }
            if(in_array($column_name,["create_time","createtime"])){
                $flag["create_time_field"]=$column_name;
            }
            if(in_array($column_name,["update_time","updatetime"])){
                $flag["update_time_field"]=$column_name;
            }
            $fields[]=[
                "name"=>$column_name,
                "alias"=>"",
                "title"=> $column["COLUMN_COMMENT"] ? $column["COLUMN_COMMENT"] : $column_name ,
                "describe"=>$column["COLUMN_COMMENT"],
                "data_type"=>$column["DATA_TYPE"],
                "max_length"=>$column["CHARACTER_MAXIMUM_LENGTH"],
                "is_nullable"=>$column["IS_NULLABLE"]==="YES",
                "type"=>"",
                "type_params"=>"",
            ];
        }
        return ["table"=>$table,"fields"=>$fields,"flag"=>$flag];
    }
}
